package gui;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import javax.imageio.ImageIO;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;


import core.Player;

/**
 * 
 * @author Oscar
 * The panel that shows the cards that the player has on his hand. Might also be used for menu buttons and such.
 */
public class HandViewPanel extends JPanel implements ActionListener, Observer{
	
	Point latestPoint = new Point(0,0);
	Point currentPoint = new Point(0,0);
	Timer Mouseovertimer = new Timer(50, (ActionListener) this);
	private Player owningPlayer;
	List<CardButton> cardButtons = new ArrayList<CardButton>();
	private JPanel cardPanel = new JPanel();
	private Dimension cardPanelDimension = new Dimension(250, 200);
	private boolean uglySwitch = false;
	
	private static HandViewPanel me;
	
	public static HandViewPanel getInstance(){
		if(me == null)
			me = new HandViewPanel();
		return me;
	}
	
	public HandViewPanel(){
		super();
		initHVP();
	}
	
	private void initHVP(){
		setOpaque(true);
		
	}

	/**
	 * A method mostly used for testing, since we want to have different hands for different players. In the game
	 * in it's final version however, the idea is that there is only one player/computer.
	 * @param p
	 */
	public void setOwningPlayer(Player p){
		if(owningPlayer != p){
			owningPlayer = p;
			changePlayerHandView();
			this.updateUI();
			this.repaint();
		}
	}
	
	/**
	 * Method that changes the view in the HandViewPanel to the correct cards of the current player. The method
	 * removes the current CardButtons and adds new ones for the current player. This method is moslty used for
	 * testing.
	 */
	private void changePlayerHandView(){
		if(uglySwitch == false){
			uglySwitch = true;
			JLabel filler = new JLabel();
			filler.setPreferredSize(new Dimension(100,5));
			add(filler);
			cardPanel.setPreferredSize(cardPanelDimension);
			cardPanel.setOpaque(false);
			add(cardPanel);
		}else{
			cardPanel.removeAll();
			for(int i = 0; i < cardButtons.size(); i++)
				cardButtons.remove(0);
		}
		for(int i = 0; i<owningPlayer.getHand().size(); i++){
			cardButtons.add(new CardButton(owningPlayer.getHand().get(i)));
			addCardToPanel();
		}
	}
	
	
	private void addCardToPanel(){
		cardPanel.add(cardButtons.get(cardButtons.size()-1));
		cardButtons.get(cardButtons.size()-1).setToolTipText("Gold cost: " + cardButtons.get(cardButtons.size()-1).getCard().getGoldCost());
		cardButtons.get(cardButtons.size()-1).addActionListener(this);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		for(int i = 0; i<cardButtons.size(); i++){
			if(e.getSource() == cardButtons.get(i))
				SelectionController.getInstance().select(cardButtons.get(i).getCard());
		}
	}

	@Override
	public void update(Observable arg0, Object arg1) {
		changePlayerHandView();
		updateUI();
		repaint();
	}
	
	

	
	/**
	 * Method used for painting the background of this panel.
	 */
	protected void paintComponent(Graphics g){
		
		BufferedImage img = null;
		try {
		    img = ImageIO.read(new File("images/backgroundReal.jpg"));
		} catch (IOException e) {
		}
		
		g.drawImage(img , 0,0,this.getWidth(),this.getHeight(),this);
	}
}
